const dns = require('dns'),
      func = require('./common.js');
var RES,_GET,h;
exports.main = (req,res) =>{
    var tool = require(req.bind.dir+'/module/emma.js')
    var HTML = tool.require('html');
    _GET = req.bind.__GET;
    RES = res;
    h = new HTML();
    h.isMobile(req,tool);
    h.load({'title':'DNS || '+Math.random()*100000});
    //req.setEncoding('utf-8');//无效相比=  h.appHead('<meta charset="utf-8">');
    h.appHead('<meta charset="utf-8">');
    var html = '';
    html += func.navlist(func.udata(req.url,'app'));
    html += egFunc();
    h.appBody(html);
    res.write(h.getHtml());
    echo('<p>回调函数难以返回数据！(异步)</p>');
    //  清除函数缓存
    func.clear(__dirname+'/common.js');
}
function egFunc(){
    var xhtml = '',
        svStr = '',
        data
    ;
    var data = dns.getServers();
    for(var k in data){
        svStr += '<br>'+data[k];
    }
    xhtml += '<p>'+svStr+'</p>';
    /*
    //  异步函数/回调函数-不可返回值
    dns.lookup('baidu.com',(err, addresses, family)=>{
        console.log(addresses);
    });
    */
    //console.log(RES);
    return xhtml;
}

function echo(xhtml){
    if(typeof(xhtml) == 'undefined' || xhtml == null || xhtml == '') return false;
    if(typeof(RES) != 'object') return false;
    RES.write(xhtml);
}
//  打印object数据对象
function pobj(data,all)
{
    var xhtml = '';
    if(typeof(data) == 'object'){
        for(var k in data){
            if(typeof(data[k]) != 'object' && typeof(data[k]) != 'function') xhtml += `<li>${k} : ${data[k]}</li>`;
            else if(all) xhtml += `<li><br>${k} : ${data[k]}</li>`;
        }
    }
    return xhtml;
}
